<template>
  <view>
    <!-- #ifdef APP-PLUS -->
    <uni-list>
      <uni-list-item
        title="推送通知"
        note="打开/关闭应用推送通知"
        :showSwitch="true"
        @switchChange="switchChange"
        :switchChecked="isOn"
      >
      </uni-list-item>
    </uni-list>
    <!-- #endif -->
    <uni-list>
      <uni-list-item title="退出登录" :showArrow="true" :clickable="true" @click="logoutHandler"></uni-list-item>
    </uni-list>
  </view>
  <uni-popup ref="confirm">
    <uni-popup-dialog title="确认退出登录吗？" @close="cancelHandler" @confirm="logout"></uni-popup-dialog>
  </uni-popup>
</template>

<script setup>
  import { ref } from 'vue'
  import { userStore } from '@/store/user.js'
  import { globalStore } from '@/store/global.js'

  const useUserStore = userStore()
  const useGlobalStore = globalStore()
  const confirm = ref()
  // #ifdef APP-PLUS
  let main = plus.android.runtimeMainActivity()
  let manager = plus.android.invoke('com.igexin.sdk.PushManager', 'getInstance')
  let isOn = plus.android.invoke(manager, 'isPushTurnedOn', main)
  const isPush = ref(isOn)

  // 测试推送通知
  const testPush = () => {
    console.log(isOn)
    uni.createPushMessage({
      content: 'test',
      title: 'test'
    })
  }

  // 打开/关闭推送通知
  const switchChange = () => {
    isPush.value = !isPush.value
    if (isPush.value) {
      openPush()
    } else {
      closePush()
    }
    // console.log(isPush);
  }

  // 开启推送
  const openPush = () => {
    plus.android.invoke(manager, 'turnOnPush', main)
  }

  // 关闭推送
  const closePush = () => {
    plus.android.invoke(manager, 'turnOffPush', main)
  }
  // #endif

  const logout = () => {
    useUserStore.destroyToken()
    uni.reLaunch({
      url: '/pages/login/login'
    })
  }
  // 退出登录
  function logoutHandler() {
    confirm.value.open()
  }
  // 取消退出登录
  function cancelHandler() {
    confirm.value.close()
  }
</script>

<style></style>
